#include "Box.h"

Box::Box ( CoordF pos, GLfloat w, GLfloat h, GLfloat d )
{
  setDimensions(w, h, d);
  setPos(pos);
}

Box::Box ( CoordF pos, GLfloat w, GLfloat h, GLfloat d, CoordF c )
{
  setDimensions(w, h, d);
  setPos(pos);
  setColor(c);
}

Box::Box ( CoordF pos, GLfloat w, GLfloat h, GLfloat d, int c )
{
  setDimensions(w, h, d);
  setPos(pos);
  setColor(c);
}

Box::Box ( CoordF pos, GLfloat w, GLfloat h, GLfloat d, const char * tex )
{
  setDimensions(w, h, d);
  setPos(pos);
  setTex(tex);
}

Box::Box ( CoordF pos, GLfloat w, GLfloat h, GLfloat d, const char * texarray[6] )
{
  setDimensions(w, h, d);
  setPos(pos);
  setTex(texarray);
}

void Box::setDimensions( GLfloat w, GLfloat h, GLfloat d )
{
  // Dimensiona as faces da caixa
  sides[Box::FRONT].setSize(w, h);
  
  sides[Box::LEFT].setSize(d, h);
  sides[Box::LEFT].rotate( CoordF( 0, 270, 0 ) );
  
  sides[Box::BACK].setSize(w, h);
  sides[Box::BACK].rotate( CoordF( 0, 180, 0 ) );
  
  sides[Box::RIGHT].setSize(d, h);
  sides[Box::RIGHT].rotate( CoordF( 0, 90, 0 ) );
  
  sides[Box::TOP].setSize(w, d);
  sides[Box::TOP].rotate( CoordF( 270, 0, 0 ) );
  
  sides[Box::BOTTOM].setSize(w, d);
  sides[Box::BOTTOM].rotate( CoordF( 90, 0, 0 ) );
  
  width = w;
  height = h;
  depth = d;
}

void Box::setPos ( CoordF newPos )
{
  CoordF tmp = Settings::unitVectorZ;
  tmp *= depth/2;
  sides[Box::FRONT].setPosCenter(newPos + tmp);
  
  tmp = Settings::unitVectorX;
  tmp *= -width/2;
  sides[Box::LEFT].setPosCenter(newPos + tmp);
  
  tmp = Settings::unitVectorZ;
  tmp *= -depth/2;
  sides[Box::BACK].setPosCenter(newPos + tmp);

  tmp = Settings::unitVectorX;
  tmp *= width/2;
  sides[Box::RIGHT].setPosCenter(newPos + tmp);

  tmp = Settings::unitVectorY;
  tmp *= height/2;
  sides[Box::TOP].setPosCenter(newPos + tmp);

  tmp = Settings::unitVectorY;
  tmp *= -height/2;
  sides[Box::BOTTOM].setPosCenter(newPos + tmp);

  pos = newPos;
}

void Box::setTex ( const char * tex )
{
  for (int i=0; i<6; i++)
    sides[i].material()->setMaterial(tex);
}

void Box::setTex ( const char * texarray[6] )
{
  for (int i=0; i<6; i++)
    sides[i].material()->setMaterial(texarray[i]);
}

void Box::setColor ( CoordF c )
{
  for (int i=0; i<6; i++)
    sides[i].material()->setMaterial(c);
}

void Box::setColor ( int c )
{
  for (int i=0; i<6; i++)
    sides[i].material()->setMaterial(c);
}

void Box::draw()
{
  for (int i=5; i>=0; i--)
    sides[i].draw();
}
